import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper_app/components/Images/image_list_base.dart';
import 'package:wallpaper_app/components/windows/window_title_bar.dart';
import 'package:wallpaper_app/db/love_database.dart';

class LovePage extends StatefulWidget {
  final String axis;
  const LovePage({super.key, required this.axis});

  @override
  State<LovePage> createState() => _LovePageState();
}

class _LovePageState extends State<LovePage> {
  List imgs = [];

  void getData() async {
    var res =
        await context.read<LoveDatabase>().getAllLove('image', widget.axis);
    setState(() {
      imgs = res;
    });
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: SizedBox(
        height: 45,
        width: 45,
        child: FloatingActionButton(
          heroTag: null,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50),
          ),
          child: Icon(Icons.delete, color: Colors.red),
          onPressed: () {
            showCupertinoDialog(
              //点击空白处取消
              barrierDismissible: true,
              context: context,
              builder: (context) {
                return CupertinoAlertDialog(
                  title: Text('提示'),
                  content: Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: Text('确认删除全部图片吗？'),
                  ),
                  actions: [
                    CupertinoDialogAction(
                      child: Text('取消'),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    CupertinoDialogAction(
                      child: Text('确认'),
                      onPressed: () {
                        context
                            .read<LoveDatabase>()
                            .removeAllLove('image', widget.axis);
                        setState(() {
                          imgs = [];
                        });
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                );
              },
            );
          },
        ),
      ),
      body: Column(
        children: [
          WindowTitleBar(isBack: true),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(1),
              // ignore: prefer_is_empty
              child: ImageListBase(imageList: imgs, isLoading: false),
            ),
          ),
        ],
      ),
    );
  }
}
